// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.service.user;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import fr.upstlse.glre.business.domain.user.User;
import fr.upstlse.glre.business.domain.user.UserDao;
import fr.upstlse.glre.business.vo.user.UserSearchCriteriaVO;
import fr.upstlse.glre.business.vo.user.UserVO;

/**
 * @see fr.upstlse.glre.business.service.user.UserService
 */
public class UserServiceImpl
    extends fr.upstlse.glre.business.service.user.UserServiceBase
{

    /**
     * {@inheritDoc}
     * Return the registered user if exits. Create it otherwise.
     */
	@Override
	protected long handleCreateUser(UserVO pUserVO) throws Exception {
		//search for login
		UserVO lUserVOCreated = (UserVO) getUserDao().findByLogin(UserDao.TRANSFORM_USERVO, pUserVO.getLogin());
		if(lUserVOCreated == null) {
			lUserVOCreated = (UserVO) getUserDao().create(UserDao.TRANSFORM_USERVO, getUserDao().userVOToEntity(pUserVO));
		}
		
		return lUserVOCreated.getId();
	}

	@Override
	protected void handleDeleteUser(UserVO pUserVO) throws Exception {
		getUserDao().remove(getUserDao().userVOToEntity(pUserVO));
	}

	@Override
	protected UserVO handleGetById(long pUserId) throws Exception {
		return (UserVO) getUserDao().load(UserDao.TRANSFORM_USERVO, pUserId);
	}

	@Override
	protected List handleFindUser(UserSearchCriteriaVO pUserSearchCriteriaVO)
			throws Exception {
		List<User> lUsersFinded = getUserDao().findByCriteria(pUserSearchCriteriaVO);
		
		List<UserVO> lUsersVOFinded = new ArrayList<UserVO>();
		Iterator<User> lUserIterator = lUsersFinded.iterator();
		while (lUserIterator.hasNext()) {
			lUsersVOFinded.add(getUserDao().toUserVO(lUserIterator.next()));
		}
		
		return lUsersVOFinded;
	}

	@Override
	protected void handleUpdateUser(UserVO pUserVO) throws Exception {
		getUserDao().update(getUserDao().userVOToEntity(pUserVO));
	}

	@Override
	protected List handleGetAllUsers() throws Exception {
		Collection<UserVO> lUsers = getUserDao().loadAll(
				UserDao.TRANSFORM_USERVO);
		ArrayList<UserVO> lUsersArrayList = new ArrayList<UserVO>(lUsers);

		return lUsersArrayList;
	}

	@Override
	public List findUser(UserSearchCriteriaVO pUserSearchCriteriaVO) {
		try
        {
            return this.handleFindUser(pUserSearchCriteriaVO);
        }
        catch (Throwable th)
        {
            throw new fr.upstlse.glre.business.service.user.UserServiceException(
                "Error performing 'fr.upstlse.glre.business.service.user.UserService.findUser(fr.upstlse.glre.business.vo.user.UserSearchCriteriaVO pUserSearchCriteriaVO)' --> " + th,
                th);
        }
	}

	@Override
	protected UserVO handleCreateAndGetUser(UserVO pUserVO) throws Exception {
		//search for login
		UserVO lUserVOCreated = (UserVO) getUserDao().findByLogin(UserDao.TRANSFORM_USERVO, pUserVO.getLogin());
		if(lUserVOCreated == null) {
			lUserVOCreated = (UserVO) getUserDao().create(UserDao.TRANSFORM_USERVO, getUserDao().userVOToEntity(pUserVO));
		}
		return lUserVOCreated;
	}

	@Override
	protected UserVO handleFindByLogin(String pLogin) throws Exception {
		UserVO lUserVO = (UserVO) getUserDao().findByLogin(UserDao.TRANSFORM_USERVO, pLogin);
		return lUserVO;
	}
}